|
1
|
|
|
import * as fs from 'fs' |
|
2
|
|
|
import { getTagsFromId3Tag } from '../id3-tag' |
|
3
|
|
|
import { isFunction, isString } from '../util' |
|
4
|
|
|
import { Tags, TagIdentifiers } from '../types/Tags' |
|
5
|
|
|
import { Options } from '../types/Options' |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Callback signature for successful asynchronous read operation. |
|
9
|
|
|
* |
|
10
|
|
|
* @param tags - `TagsIdentifiers` if the `rawOnly` option was true otherwise |
|
11
|
|
|
* `Tags` |
|
12
|
|
|
* @public |
|
13
|
|
|
*/ |
|
14
|
|
|
export type ReadSuccessCallback = |
|
15
|
|
|
(error: null, tags: Tags | TagIdentifiers) => void |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Callback signatures for failing asynchronous read operation. |
|
19
|
|
|
* |
|
20
|
|
|
* @public |
|
21
|
|
|
*/ |
|
22
|
|
|
export type ReadErrorCallback = |
|
23
|
|
|
(error: NodeJS.ErrnoException | Error, tags: null) => void |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Callback signatures for asynchronous read operation. |
|
27
|
|
|
* |
|
28
|
|
|
* @public |
|
29
|
|
|
*/ |
|
30
|
|
|
export type ReadCallback = |
|
31
|
|
|
ReadSuccessCallback & ReadErrorCallback |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Reads ID3-Tags synchronously from passed buffer/filepath. |
|
35
|
|
|
* |
|
36
|
|
|
* @public |
|
37
|
|
|
*/ |
|
38
|
|
|
export function read(filebuffer: string | Buffer, options?: Options): Tags |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Reads ID3-Tags asynchronously from passed buffer/filepath. |
|
42
|
|
|
* |
|
43
|
|
|
* @public |
|
44
|
|
|
*/ |
|
45
|
|
|
export function read(filebuffer: string | Buffer, callback: ReadCallback): void |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Reads ID3-Tags asynchronously from passed buffer/filepath. |
|
49
|
|
|
* |
|
50
|
|
|
* @public |
|
51
|
|
|
*/ |
|
52
|
|
|
export function read( |
|
53
|
|
|
filebuffer: string | Buffer, options: Options, callback: ReadCallback |
|
54
|
|
|
): void |
|
55
|
|
|
|
|
56
|
|
|
export function read( |
|
57
|
|
|
filebuffer: string | Buffer, |
|
58
|
|
|
optionsOrCallback?: Options | ReadCallback, |
|
59
|
|
|
callback?: ReadCallback |
|
60
|
|
|
): Tags | TagIdentifiers | void { |
|
61
|
|
|
const options: Options = |
|
62
|
|
|
(isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {} |
|
63
|
|
|
callback = |
|
64
|
|
|
isFunction(optionsOrCallback) ? optionsOrCallback : callback |
|
65
|
|
|
|
|
66
|
|
|
if (isFunction(callback)) { |
|
67
|
|
|
return readAsync(filebuffer, options, callback) |
|
68
|
|
|
} |
|
69
|
|
|
return readSync(filebuffer, options) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
function readSync(filebuffer: string | Buffer, options: Options) { |
|
73
|
|
|
if (isString(filebuffer)) { |
|
74
|
|
|
filebuffer = fs.readFileSync(filebuffer) |
|
75
|
|
|
} |
|
76
|
|
|
return getTagsFromId3Tag(filebuffer, options) |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
function readAsync( |
|
80
|
|
|
filebuffer: string | Buffer, |
|
81
|
|
|
options: Options, |
|
82
|
|
|
callback: ReadCallback |
|
83
|
|
|
) { |
|
84
|
|
|
if (isString(filebuffer)) { |
|
85
|
|
|
fs.readFile(filebuffer, (error, data) => { |
|
86
|
|
|
if(error) { |
|
87
|
|
|
callback(error, null) |
|
88
|
|
|
} else { |
|
89
|
|
|
callback(null, getTagsFromId3Tag(data, options)) |
|
90
|
|
|
} |
|
91
|
|
|
}) |
|
92
|
|
|
} else { |
|
93
|
|
|
callback(null, getTagsFromId3Tag(filebuffer, options)) |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|